﻿using System;
using System.Data;
using System.Configuration;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

using System.IO;
using System.ComponentModel;
using System.Collections;
using System.Text.RegularExpressions;
using System.Text;

namespace TA.Turbo.Controls
{
	/// <summary>
	/// Extends <see cref="MultiPartValue"/> by allowing to add and remove <see cref="ListItem"/>s.
	/// </summary>
	public class ListControlMultiPartValue : MultiPartValue
	{
		#region PUBLIC
		#region INIT
		/// <summary>
		/// Ctor.
		/// </summary>
		/// <param name="listControl"></param>
		public ListControlMultiPartValue(IMultiValueListControl listControl)
		{
			this._listControl = listControl;
		}
		#endregion
		#region PROPS
		/// <summary>
		/// Gets a modifiable collection of <see cref="ListItem"/>s.
		/// </summary>
		public List<ListItem> SelectedItems
		{
			get
			{
				if (_selectedItems == null)
				{
					_selectedItems = new List<ListItem>();

					foreach (string v in GetValueList(_value))
					{
						_selectedItems.Add(this._listControl.Items.FindByValue(v));
					}
				}
				return _selectedItems;

			}
		}
		/// <summary>
		/// Gets a modifiable collection of indices of <see cref="ListItem"/>s.
		/// </summary>
		public List<int> SelectedIndices
		{
			get
			{
				if (this._selectedIndices == null)
				{
					this._selectedIndices = new List<int>();
					ListItem[] options = this._listControl.Options;

					foreach (string v in GetValueList(_value))
					{
						for (int i = 0; i < options.Length; ++i)
						{
							if (string.Equals(options[i].Value, v))
							{
								this._selectedIndices.Add(i);
								break;
							}
						}
					}
				}
				return this._selectedIndices;
			}
		}
		/// <summary>
		/// Gets or sets comma-separated list of values.
		/// </summary>
		public override string Value
		{
			get
			{
				if (_selectedItems != null)
				{
					StringBuilder sb = new StringBuilder();
					foreach (ListItem li in _selectedItems)
					{
						if (sb.Length > 0) sb.Append(",");
						sb.Append(li.Value);
					}
					base.Value = sb.ToString();
				}
				else if (this._selectedIndices != null)
				{
					StringBuilder sb = new StringBuilder();
					ListItem[] options = this._listControl.Options;

					foreach (int index in this._selectedIndices)
					{
						if (sb.Length > 0) sb.Append(",");
						sb.Append(options[index].Value);
					}

					base.Value = sb.ToString();
				}
				return base.Value;
			}
			set
			{
				base.Value = value;
			}
		}
		#endregion
		#endregion
		#region INTERNAL
		#region VARS
		List<ListItem> _selectedItems;
		List<int> _selectedIndices;
		IMultiValueListControl _listControl;
		#endregion
		#region METHODS
		/// <summary>
		/// Nullifies colleciton of <see cref="ListItem"/>s.
		/// </summary>
		protected override void NullifyLists()
		{
			base.NullifyLists();
			this._selectedItems = null;
			this._selectedIndices = null;
		}
		#endregion
		#endregion
	}
}